package im.oen.gui.tools.util;

import im.oen.boot.common.utils.Checker;
import im.oen.boot.common.utils.Runner;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * @author Kevin.Xu
 */
public class MsgUtil {

    private static MessageSource MESSAGE_SOURCE;

    public static void init(MessageSource messageSource) {
        MsgUtil.MESSAGE_SOURCE = messageSource;
    }

    public static String get(final String key) {
        return Runner.safeRun(() -> {
            return MESSAGE_SOURCE.getMessage(key, null, LocaleContextHolder.getLocale());
        }, key);
    }

    public static String suffix(final String key, final String suffix) {
        return get(key) + (Checker.isEmpty(suffix) ? "" : suffix);
    }

    public static String prefix(final String key, final String prefix) {
        return (Checker.isEmpty(prefix) ? "" : prefix) + get(key);
    }
}
